<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue-resource get post http 全局拦截</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="../node_modules/vue/dist/vue.js"></script>
    <script src="../node_modules/vue-resource/dist/vue-resource.js"></script>
</head>

<body>
    <div id="app" class="container">
        <h2>vue-resource插件</h2>
        <a href="javascript:;" class="btn btn-primary" @click="get">Get请求</a>
        <a href="javascript:;" class="btn btn-primary" @click="post">Post请求</a>
        <a href="javascript:;" class="btn btn-primary">JSONP请求</a>
        <a href="javascript:;" class="btn btn-primary" @click="http">http请求</a>
        <br/>
        <span>{{msg}}</span>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                msg: '',
            },
          mounted:function () {
            Vue.http.interceptors.push(function (request,next) {
              console.log("request init.");
              next(function (response) {
                console.log("response init.");
                return response;
              })
            })
          },
          http:{
            root:"http://localhost:63342/new_demo/"
          },
            methods: {
                get: function() {
                    this.$http.get("package.json", {
                        params: {
                            userId: "Sky-Blue"
                        },
                        headers: {
                            token: "The get token"
                        }
                    }).then(res => {
                        this.msg = res.data;
                    }, error => {
                        this.msg = error;
                    });
                },
                post: function() {
                    this.$http.post("package.json", {
                        userId: "Sky-Blue"
                    }, {
                        headers: {
                            access_token: "The post token"
                        }
                    }).then(res => {
                        this.msg = res.data
                    }, error => {
                        this.msg = error
                    });
                },
              // jsonp:function () {
              //   this.$http.jsonp()
              // }
              http:function () {
                this.$http({
                  url:"package.json",
                  params:{
                    userId:"Sky-Blue"
                  },
                  headers:{
                    token:"The http token"
                  },
                  before:function () {
                    console.log("before init.")
                  }
                }).then(function (res) {
                  this.msg=res.data;
                })
              }
            }
        })
    </script>
</body>

</html>
